Import "shanhai.lua"
Import "FileEx.lua"
//游戏王DL刷路人脚本，开发分辨率1080*1920,320DPI
//全局变量定义
Dim If_There_Is//判断当前屏幕的决斗者点完了没有
Dim P_Flag//检测人像是否存在，在无限模式中会被强制置为1
Dim intx,inty,x1,y1,x2,y2,FuncBack,NOTHING,a,b,Now_Screen,t,m,n,inta,intb,j,k,switch_world
Dim m1,n1,m2,n2
Dim User_Choosing_Temp//这一行用来定义临时表格变量
Dim User_Choosing(2)//这一行用来定义从表格里面提取出来的值
Dim Auto_Type(6,2)//定义挂机模式，第一维0表示刷一次路人，1表示无限路人，2表示一次路人+无限传送门，3表示无限传送门,4表示不切换世界无限路人；第二维0表示传送门的单人模式，1表示循环模式
//初始化
m = 475//m为进入决斗后点击的横坐标，该值能点到重新开始的取消
n = 1095//n为进入决斗后点击的纵坐标
m1=10
n1=1900//返回点,左下角
m2=520//能够防止点到取消
n2=1095
//主过程
For j = 0 To 3
    For k = 0 To 1
        Auto_Type(j,k)=0
    Next
Next
SetScreenScale 1080,1920
thread.start(error)
delay 200
thread.start(halt)
Call First_UI()
thread.start(error)
delay 200
thread.start(halt)
delay 500
If Auto_Type(0, 0) = 1 Then 
    switch_world=1
    Call 一次路人()
End if
If Auto_Type(1, 0) = 1 Then 
    switch_world=1
    Call 无限路人()
End if
If Auto_Type(2, 0) = 1 Then 
    switch_world = 0
    Call 一路无限传(0)
End If
If Auto_Type(2, 1) = 1 Then 
    switch_world = 0
    Call 一路无限传(1)
End If
If Auto_Type(3, 0) = 1 Then
    Call 无限传送门(0)
End If
If Auto_Type(3, 1) = 1 Then
    Call 无限传送门(1)
End If
If Auto_Type(4, 0) = 1 Then 
    switch_world = 0
    Call 无限路人()
End If
If Auto_Type(5, 0) = 1 Then 
    Call 翻邮件()
End If

Sub 翻邮件
    Do
        Tap 535, 240
        Delay 1000
        If CmpColorEx("400|751|332211,738|753|47330E,537|1150|923909", 0.9) = 1 Then 
            for 4
                Tap 308, 1709
                Delay 400
            next
        End If
    Loop
End Sub

Sub 一次路人()
    call back_to_mainUI()
    call FindDuelist()
    Do
        Call Ren_xiang()
        if p_flag =1
            Call FindDuelist()
            Call gems()
            Call Switch_Screen(now_screen)
        end if 
        if P_flag = 2 Then 
            call back_to_mainUI()
        End If
        If P_flag = 0 Then 
            ShowMessage "一次路人结束", 100000, 100, 500
        End If
    Loop While P_flag <>0
End Sub

Sub 一路无限传(x)
    ShowMessage "先刷一次路人"
    Call 一次路人()
    ShowMessage "现在开始打传送门", 100000, 100, 500
    Call 无限传送门(x)
End Sub

Sub 无限路人()
    Do
        call FindDuelist()
        call gems()
        call Switch_Screen(now_screen)
    Loop
End Sub

sub back_to_mainUI()
    do
        if CmpColorEx("402|1834|05056F-101010,336|1798|C9DCFF-101010,744|1812|090655-101010", 0.9) = 1 Then //待替换
            Tap 540,1850
            delay 500
            ShowMessage "red hat canceled.",1000,300,300
        end if
		
        if CmpColorEx("63|507|000000,63|506|060606,64|506|6A6664,843|500|52FAFF", 1) = 1 Then //待替换
            Tap 540,1650
            delay 500
            ShowMessage "daily reward.",1000,300,300
        end if
		
        delay 500
        tap m1,n1
           
        if CmpColorEx("923|114|FFFFFF,634|117|45E1FF,911|128|851385",1)=1 
            delay 300
            if CmpColorEx("923|114|FFFFFF,634|117|45E1FF,911|128|851385",1)=1 
                delay 300
                if CmpColorEx("923|114|FFFFFF,634|117|45E1FF,911|128|851385",1)=1 
                    exit do
                end if
            end if
        end if
    loop
end sub

Sub FindDuelist()
    If_There_Is = 1
    Dim a1,a2,a3,a4,b1,b2,c1,c2,d1,d2
    call judge_screen()
    Do while If_There_Is=1
        FindMultiColor 6, 970, 1061, 1787, "FFFFFE -101010", "2|2|FFFFE6-101010,1|12|EBDA1D-101010,1|29|F0F0F1-101010", 0, 1, a1, a2
        FindMultiColor 6, 970, 1061, 1787, "DFCE03-101010", "3|0|FFFFEC-101010,2|3|EDDC21-101010",0,1,a3,a4
        FindColor 6, 970, 1061, 1787, "FD5C33|365BFD|FF7733|FE5533", 0, 1, b1, b2
        FindColor 6, 970, 1061, 1787, "0055FF", 0, 1, c1, c2
        FindColor 6, 970, 1060, 1787, "4444BC", 0, 1, d1, d2
        //917，1298以及784，1410是盗贼基斯的帽子,和红帽冲突
        If (d1 = 917 and d2 = 1298) or (d1=784 and d2=1410) Then 
            FindColor 6, 970, 783, 1787, "4444BC", 0, 1, d1, d2
            If (d1 <= -1 and d2 <= -1) Then 
                FindColor 6, 970, 1060, 1297, "4444BC", 0, 1, d1, d2
            End If
        End If
        If (a1 > -1 and a2 > -1) Then           
            if Now_Screen=2 and a1>590 and a2>1120 and a2<1300
                a1=a1-30
            end if
            Tap a1-20, a2 +30
            ShowMessage "普通路人",1000,300,300
            Delay 1000                         
            Call Duel_Process()
        ElseIf a3 > -1 and a4 > -1 Then
            if Now_Screen=2 and a3>590 and a4>1120 and a4<1300
                a3=a3-30
            end if
            Tap a3 -20, a4 + 30
            ShowMessage "type 1.5",1000,300,300
            Delay 1000                                
            Call Duel_Process()
        ElseIf b1 > -1 and b2 > -1 Then
            Tap b1 -15, b2 + 30
            ShowMessage "传奇决斗者",1000,300,300
            Delay 1000
            Call Duel_Process()
        ElseIf c1 > -1 and c2 > -1 Then
            Tap c1 , c2 +30
            ShowMessage "type 3",1000,300,300
            Delay 1000
            Call back_to_mainUI()        
        ElseIf d1 > -1 and d2 > -1 and d2<>1573 and d1<>836  Then
            Tap d1 -15, d2 + 30
            ShowMessage "小红帽",1000,300,300
            Delay 1000
            Call Duel_Process()
        Else 
            ShowMessage "no head",1000,300,300
            If_There_Is=0
        End If 
    Loop
End Sub

sub judge_screen()
    //判断当前是第几屏幕
    If CmpColorEx("157|1837|EDBFA3,159|1848|E16430,159|1854|B83300", 0.9) = 1 Then 
        Now_Screen = 1
        ShowMessage "Found gate!Now it's the first screen",1000,120,400
    elseIf CmpColorEx("386|1825|FBE29A,392|1829|D89654,394|1835|D59B67", 0.9) = 1 Then 
        Now_Screen=2
        ShowMessage "Found arena!Now it's the second screen",1000,120,400
    elseIf CmpColorEx("612|1843|CE410F,613|1844|EFB79B,670|1844|E77C2D", 0.9) = 1 Then 
        Now_Screen=3
        ShowMessage "Found shop!Now it's the third screen",1000,120,400
    elseIf CmpColorEx("917|1848|EE8232,910|1846|DD622F,928|1849|F55513", 0.9) = 1 Then 
        Now_Screen=4
        ShowMessage "Found studio!Now it's the fourth screen",1000,120,400
    else 
        call back_to_mainUI()
        If_There_Is=0
    End If
End Sub

Sub duel_process_simple()
    Do           	                  
        Tap 540, 1066
        Delay 200
        //直到duel出现
        ShowMessage "detect duel appear",1000,300,300
    Loop until  CmpColorEx("98|1613|008089,345|1683|33494A,459|1725|798989",0.7) = 1  //待替换
    Do
        tap 270,1660
        Delay 200
        //直到duel消失
        ShowMessage "detect duel disappear",1000,300,300
    Loop until  CmpColorEx("98|1613|008089,345|1683|33494A,459|1725|798989", 0.7) <> 1  //待替换
    call auto_surrender()
End Sub
 
Sub Duel_Process()
    If CmpColorEx("1027|1426|CCBA47-101010,817|1478|B89933-101010,807|1437|AB9933-101010",0.9) = 1
        call duel_process_simple()
        call get_reward()
    end if
end sub

sub get_reward()
    Do
        Tap m1,n1
        Delay 500
        //检测next按钮是否弹出
        If  CmpColorEx("756|1851|C98F0C	747|1849|933B09	741|1863|BB790B", 0.9) = 1 
            Tap 542,1820
            Delay 500
            //检测Next是否消失。此处和之前不一样，之前按钮点了之后不会消失，只能通过下一步来判断操作是否被执行；但是在这里，只要按钮消失则说明操作生效，可以减少不必要的多余操作
            If CmpColorEx("756|1851|C98F0C	747|1849|933B09	741|1863|BB790B", 0.9) <> 1
                Exit Do       
            End If//检测消失的内容必须嵌套在成功的条件之内，不然如果第一次没检测到就直接跳出循环      
        End If        
        ShowMessage "detect next appear", 1000, 300, 300        
    Loop
                            
    Do
        Tap m1,n1
        Delay 500
        if cmpcolorex("750|1085|FFFFFF-101010,347|1118|FFFFFF-101010",0.9)=1 then  //待替换
            tap m1,n1
            delay 100
            tap m1,n1
            delay 500
        end if
        //这里的重复点是有意义的，可以减少OK出现前的等待时间
        //检测NEXT后的OK是否弹出
        FindMultiColor 299, 1723, 810, 1901, "F7A719", "21|3|A67105,414|-1|D57200", 0, 0.9, intX, intY  //待替换
        If intx>-1 and inty>-1 then
            Tap intx+50, inty+50
            Delay 1000
            Exit Do
        End If
        ShowMessage "detect ok appear",1000,300,300
    Loop

    Do
        //检测OK是否消失
        FindMultiColor 299, 1723, 810, 1901, "F7A719", "21|3|A67105,414|-1|D57200", 0, 0.9, intX, intY  //待替换
        If intx = -1 Then 
            Exit Do
            Else Tap intx+50,inty+50
        End If 	
        ShowMessage "detect ok disappear",1000,300,300
    Loop  
    ShowMessage "战斗结束", 300, 300, 1000
    Call back_to_mainUI()
    Delay 500//稍微做延迟防止误点击
    Call back_to_mainUI()//结束和路人对话
End Sub

//换界面
Sub Switch_Screen(x)
    Select Case x
    Case 1
        Tap 426, 1868
        Delay 2000
    Case 2
        Tap 682, 1868
        Delay 2000
    Case 3
        Tap 915, 1862
        Delay 2000
    Case 4
        If (switch_world = 1) Then 
            Do
                Tap 862, 1619
                Delay 100
            Loop Until CmpColor(159, 125, "5F2809", 0.9) <> -1
            Delay 500
            Tap 205, 149
            Delay 1500
            If CmpColor(127, 1251, "00CCFF", 0.9) <> -1 Then 
                ShowMessage "正切换到GX世界"
                Delay 200
                Tap 134, 1379
            Else 
                ShowMessage "正切换到DL世界"
                Delay 200
                Tap 127,1251
            End If
            Call back_to_mainUI()
        End If    	
        Tap 175, 1862
        Delay 2000
    End Select
End Sub

//x=1表示循环模式
Sub 无限传送门(x)
    Do 
        Do
            FindMultiColor 31,1778,333,1919,"E54C00-202020","4|-1|FFFFFF-202020,42|7|E54C00-202020,41|10|FFFFFF-202020,85|9|E54C00-202020",0,0.7,intX,intY//从图样进入避免点击返回
            If intX > -1 And intY > -1 Then 
                ShowMessage "发现传送门，现在进入"
                Delay 200
                Tap intX, intY
                Delay 1000
            Else
                Tap 110, 1861//切换到第一屏幕
                Delay 1000
            End If
            FindMultiColor 31,1778,333,1919,"E54C00-202020","4|-1|FFFFFF-202020,42|7|E54C00-202020,41|10|FFFFFF-202020,85|9|E54C00-202020",0,0.9,intX,intY
        Loop Until intX = -1
        
        ShowMessage "已经进入传送门"
        Delay 500
        
        Do
            Delay 500
            //判断是否需要切换            
            If x=1 Then 
                ShowMessage "选择了循环模式，需要切换", 1000, 300, 300
                Delay 500
                Tap 1050,925
                Delay 500
            End If
            //寻找决斗按键
            FindMultiColor 293, 1609, 789, 1726, "35EAF6-202020", "26|-1|197F87-202020", 0, 0.9, intX, intY
        Loop Until intx <> -1
        
        Do
            ShowMessage "现在点击决斗"
            Tap 538, 1660
            Delay 500
            If CmpColor(900, 1100, "803208-202020", 0.9) <> -1 Then 
                Tap 900,1100
                Delay 500
            End If
        Loop Until CmpColor(1000,60,"FFFFFF",0.9)<>-1
        
        ShowMessage "已经进入决斗"
        Delay 500
        call auto_surrender()
        call get_reward()
    Loop
End Sub

//监测左上角人像的数量
Sub Ren_Xiang()
    If CmpColorEx("166|99|FFFFFF,174|98|FFFFFF,169|130|FFFFFF", 1) =1 Then 
        p_flag=1
    elseIf CmpColorEx("166|99|AA6666,174|98|AA6666,169|130|AA6666", 0.9) =1 Then 
        if CmpColorEx("923|114|FFFFFF,634|117|45E1FF,911|128|851385",1)=1 
            p_flag=0
        else
            p_flag=2
        end if
    else 
        p_flag=2         
    End If
End Sub

//监测停止运行后重开
Sub halt()
    do
        delay 3600000
        delay 3600000
        if CmpColorEx("520|700|1100EE,530|800|FFFFFF,534|1352|FFFF81",1)=1 
            for 3
                delay 1000
                tap 534,1352
            next
        end if
    loop
end sub

//监测网络故障后重试
Sub Error()
    Do
        Delay 10000//延迟时间（毫秒）
        Dim a
        //寻找retry按钮
        FindMultiColor 539,987,1003,1153,"FFAB1E","22|9|582205,404|3|552105,395|99|923909,409|111|FFF9D4,137|54|FFFFFF,279|46|FFFFFF",0,0.9,intX,intY  //待替换
        If intX > -1 And intY > -1 Then 
            ShowMessage "Network problem!Trying to reconnect...",2000,300,300
            Thread.SetShareVar "err_run",1
            For 3
                Delay 3000  
                Tap 788,1100
            Next
        End If
    Loop 
End Sub 

//自动投降
sub auto_surrender()
    Do
        Tap m, n
        Delay 500
        Tap m1,n1
        Delay 500
        //进入我的主阶段
        ShowMessage "detect main phrase appear", 1000, 300, 300
        
    Loop Until CmpColorEx("945|1204|FFFFFF,937|1193|4D270E,939|1140|E3BE06", 0.9) = 1  
    Do
        Tap m2,n2
        Delay 500
        //检测右上角菜单按钮是不是亮的
        FindMultiColor 904,4,1075,163,"FFAA14-101010","21|3|552200-101010,46|26|FFFFFF-101010,52|36|5D2200-101010,47|48|FFFFFF-101010,55|84|94350A-101010",0,0.9,intX,intY  
        If intX > -1 And intY > -1 Then 
            ShowMessage "surrender now!",1000,300,1300
            Tap intx, inty
            Delay 500                
        End if
        //检测投降按钮是否可以点
        FindMultiColor 428,117,945,320,"050581-101010","-176|-31|D9DFFF-101010,-165|-22|04045F-101010,275|23|060796-101010,287|30|CAD2FF-101010,95|5|FFFFFF-101010,109|3|050585-101010",0,0.9,intX,intY  //待替换
        If intx > -1 Then 
            Tap intx, inty
            Delay 500
            //检测确定的弹出
            FindMultiColor 542,968,1011,1137,"FFAA1E-101010","20|7|572205-101010,391|9|582205-101010,156|42|FFFFFF-101010,382|78|923909-101010,396|92|FFFADD-101010",0,0.9,intX,intY  //待替换
            If intX > -1 And intY > -1 Then
                Tap intx+100, inty+50
                ShowMessage "Successfully surrendered!",1000,300,1200
                Delay 200                 
            End If
        End If
        //检测OK是否弹出
    Loop Until CmpColorEx("1044|1550|036228,1049|1570|3BB174,1044|1569|037831", 0.9) = 1
    ShowMessage "Find OK"
    Delay 100
    Do
        Tap 543,1815
        Delay 500
        //循环检测OK是否消失。这里不能检测NEXT因为可能会有升级框的影响
    Loop Until  CmpColorEx("1044|1550|036228,1049|1570|3BB174,1044|1569|037831", 0.9) <> 1
End Sub

//自动点钻
sub gems()
    call judge_screen()    
    if now_screen=1
        tap 540,900
        call back_to_mainUI() 		
    elseIf now_screen=2
        tap 80,1480
        call back_to_mainUI()		
    elseIf now_screen=3
        tap 540,830
        call back_to_mainUI()		
    elseIf now_screen=4
        tap 310,1500
        call back_to_mainUI()			
        else call back_to_mainUI()
    end if
end sub

Sub First_UI()
    UI.NewLayout ("请选择挂机模式")
    UI.AddRadioGroup  ("请选择挂机模式","模式选择",{"刷一次路人","无限路人","一次路人后无限传送门","无限传送门","不切世界无限路人","翻邮件(先手动进入邮箱最后一页)"})
    traceprint UI.SetOnClose("请选择挂机模式",Read_Choice_First)
    UI.Show ("请选择挂机模式")
End sub

Sub Second_UI()
    UI.NewLayout ("请选择传送门模式")
    UI.AddRadioGroup "请选择传送门模式", "传送门模式", {"单人模式", "循环模式"}
    UI.SetOnClose "请选择传送门模式", Read_Choice_Second
    UI.show("请选择传送门模式")
End Sub

Function Read_Choice_Second()
    User_Choosing_Temp = UI.GetValue("传送门模式")
    User_choosing(1) = CInt(User_Choosing_Temp["传送门模式"])
End Function

Function Read_Choice_First()
    User_Choosing_Temp = UI.GetValue("模式选择")
    User_choosing(0) = CInt(User_Choosing_Temp["模式选择"])
    TracePrint User_Choosing
    If User_choosing(0) = 2 or User_choosing(0)= 3 Then 
        Call Second_UI()
        Auto_Type(User_choosing(0), User_choosing(1)) = 1
    Else 
        Auto_Type(User_choosing(0),0)=1
    End If
End Function
